package com.atsansan.mywork.login.controller;

import com.atsansan.mywork.login.entity.BookEntity;
import com.atsansan.mywork.login.entity.CourseraEntity;
import com.atsansan.mywork.login.service.BookService;
import com.atsansan.mywork.login.service.CourseraService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author sansan
 * @create 2021-03-12 17:23
 */
@Controller
@RequestMapping("user/book")
public class BookController {
    
    @Autowired
    BookService bookService;

    @Autowired
    CourseraService courseraService;
    @RequestMapping("/listB")
    public String list(Map<String,Object> map){
        List<BookEntity> bookList = bookService.list();

        map.put("bookList",bookList);
//        System.out.println(list.toString());
        return "listB";
    }
    @RequestMapping(value = {"/select/list/book"})
    public String selectListBook(@RequestParam("bname") String key , Map<String,Object> map){

        List<BookEntity> listSelectBook = bookService.listByKey(key);
        map.put("listSelectBook",listSelectBook);

        return "listB";
    }
//    @RequestMapping(value = {"/select/book/{key}","/select/{key}"})
//    public String selectList(@PathVariable("key") String key ,Map<String,Object> map){
//
//        List<BookEntity> listByKeyBook = bookService.listByKey(key);
//        map.put("listByKeyBook",listByKeyBook);
//        return "login_success";
//    }
    @RequestMapping(value = {"/select/book"})
    public String selectList(@RequestParam("bname") String key , Map<String,Object> map){

            List<BookEntity> listByKeyBook = bookService.listByKey(key);
            map.put("listByKeyBook",listByKeyBook);

        return "login_success";
    }

    @RequestMapping("/update/{bId}")
    public String updateById(@PathVariable("bId") Long bId,Map<String,Object> map){
        BookEntity bookEntity= bookService.getById(bId);
        map.put("bookEntity",bookEntity);
        return "updateB";
    }

    @RequestMapping("/updateB")
    public String update(BookEntity bookEntity){
        bookService.updateById(bookEntity);
        return "redirect:/user/book/listB";
    }
    @RequestMapping("/delete/{bId}")
    public String delete(@PathVariable("bId") Long bId){

        bookService.removeById(bId);
        return "redirect:/user/book/listB";
    }
}
